#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::u32;

use core_api::{app_handle, app_log};
fn main() {
    // cargo run -- list_devices
    let app_name = "ff7scre";
    app_log::config_log(app_name);

    let args = ff7scre_lib::get_args();

    match app_handle::get_app_handle(app_name) {
        Err(err) => {
            log::info!("{}", err);
            std::process::exit(-1);
        }

        Ok(handle) => {
            if args.len() > 1 {
                ff7scre_lib::init_gmm();

                let cmd = &args[1];
                match cmd.as_str() {
                    "list_devices" => {
                        let _ = ff7scre_lib::list_devices();
                    }
                    "test_audio" => {
                        if let [_, _, id, device_name, ..] = &args[..] {
                            println!("p1: {}, p2: {}", id, device_name,);
                            let _ = ff7scre_lib::test_audio(
                                id.parse::<u32>().unwrap(),
                                device_name.as_str(),
                            );
                        }
                    }
                    "test_video" => {
                        if let [_, _, id, device_name, ..] = &args[..] {
                            println!("p1: {}, p2: {}", id, device_name,);
                            let _ = ff7scre_lib::test_video(
                                id.parse::<u32>().unwrap(),
                                device_name.as_str(),
                            );
                        }
                    }

                    "start_recording" => {
                        // cargo run -- start_recording d:\\output1.mkv  薛1  false
                        // Command line arguments: ["\\\\?\\D:\\...\\ff7scre.exe", "start_recording", "d:\\output1.mkv", "薛1", "false"]
                        if let [_, _, output_file, watermark_text, force_open_camera, ..] =
                            &args[..]
                        {
                            // p1: d:\output1.mkv, p2: 薛1, p3: false
                            log::info!(
                                "start_recording, output_file: {}, watermark_text: {}, force_open_camera: {}",
                                output_file,
                                watermark_text,
                                force_open_camera
                            );

                            // ctrlc::set_handler(move || {
                            //     log::info!("recive ctr+c...");

                            //     std::process::exit(0);
                            // })
                            // .expect("set Ctrl+C handler error");

                            // std::panic::set_hook(Box::new(|panic_info| {
                            //     log::error!("panic: {:?}", panic_info);
                            // }));

                            let _ = ff7scre_lib::start_recording(
                                std::path::PathBuf::from(output_file).as_path(),
                                watermark_text.clone(),
                                force_open_camera.parse::<bool>().unwrap(),
                            );

                            //ff7scre_lib::async_example();
                        }
                    }
                    _ => (),
                }
            }

            app_handle::close_app_handle(handle);
            log::info!("{} exit", app_name);
        }
    }
}
